package com.example.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.excel.EasyExcelFactory;
import com.example.config.OpcProperties;
import com.example.entity.DbBlock;
import com.example.exception.OpcException;
import com.example.service.DbBlockService;
import lombok.extern.slf4j.Slf4j;

import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

/**
 * Copyright: Copyright (c) 2022
 *
 * <p>
 * Description:
 *
 * @author LiuFeng
 * @version 2022/11/11 11:46
 * @since
 */
@Service
@Slf4j
public class DbBlockServiceImpl implements DbBlockService {


    @Autowired
    private ResourceLoader resourceLoader;

    @Autowired
    private OpcProperties opcProperties;

    @Override
    public List<DbBlock> readSectionTwoInfo() {
        Resource resource = resourceLoader.getResource("classpath:\\file\\二标段.xlsx");
        boolean exists = Files.exists(Paths.get(opcProperties.getSectionTwo()));
        try {
            List<DbBlock> blocks;

            //若外部文件存在则，读取文件
            if (exists) {
                log.info("从{}读取数据", opcProperties.getSectionTwo());
                blocks = readDbBlocksFromFile(Files.newInputStream(Paths.get(opcProperties.getSectionTwo())));
            } else {

                blocks = readDbBlocksFromFile(resource.getInputStream());
            }
            return blocks;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<NodeId> readSectionTwoInfo1() {
        Resource resource = resourceLoader.getResource("classpath:\\file\\二标段.xlsx");
        boolean exists = Files.exists(Paths.get(opcProperties.getSectionTwo()));
        try {
            List<DbBlock> blocks;
            //若外部文件存在则，读取文件
            if (exists) {
                log.info("从{}读取数据", opcProperties.getSectionTwo());
                blocks = readDbBlocksFromFile(Files.newInputStream(Paths.get(opcProperties.getSectionTwo())));
            } else {
                blocks = readDbBlocksFromFile(resource.getInputStream());
            }
            // 将二标段DB块的变量名称，拼接成t|xxx的格式
            blocks.parallelStream().forEach(n -> n.setName("t|" + n.getName()));
            List<NodeId> nodeIdList = new ArrayList<>();
            for(DbBlock dbBlock : blocks){
                NodeId nodeId = new NodeId(1, dbBlock.getName());
                nodeIdList.add(nodeId);
            }
            return nodeIdList;
        } catch (IOException e) {
            log.error("二标段.xlsx 文件读取异常，请检查！");
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<NodeId> readSectionOneInfo() {
        Resource resource = resourceLoader.getResource("classpath:\\file\\一标段.xlsx");
        boolean exists = Files.exists(Paths.get(opcProperties.getSectionOne()));
        try {
            List<DbBlock> blocks;
            //若外部文件存在则，读取文件
            if (exists) {
                log.info("从{}读取数据", opcProperties.getSectionOne());
                blocks = readDbBlocksFromFile(Files.newInputStream(Paths.get(opcProperties.getSectionOne())));
            } else {
                blocks = readDbBlocksFromFile(resource.getInputStream());
            }
            // 将一标段DB块的变量名称，拼接成t|xxx的格式
            blocks.parallelStream().forEach(n -> n.setName("t|" + n.getName()));
            List<NodeId> nodeIdList = new ArrayList<>();
            for(DbBlock dbBlock : blocks){
                NodeId nodeId = new NodeId(1, dbBlock.getName());
                nodeIdList.add(nodeId);
            }
            return nodeIdList;
        } catch (IOException e) {
            log.error("一标段.xlsx 文件读取异常，请检查！");
            throw new RuntimeException(e);
        }
    }

    private List<DbBlock> readDbBlocksFromFile(InputStream in) throws IOException {
        return EasyExcelFactory.read(in)
                .head(DbBlock.class)
                .headRowNumber(3)
                .sheet("变量").doReadSync();
    }
}
